*******************************
*																
*  Estimation of models 1-3 									
*
*******************************

/*  

This file estimates the main models reported in the paper

Models 1 and 2 are estimated 100 times each. In each estimation, the 
ideological similarity variable that is used is based on a different 
draw of MP ideal points from their posterior distribution. The resulting 
coefficients, standard errors, and fit statistics are then combined as 
explained in the Supporting Information. The goal is to capture the 
increased uncertainty resulting from the fact that ideal points are 
not observed but estimated from observed data. 

The analysis produces several data files, which are necessary to 
reproduce the analyses in some of the other replication files.

*/

cd "..."

use data.dta, clear


* Required variable lists

local ideol "dist_med_part dist_med_kpart"	// names of ideology vars

#delimit ;								// non-ideol vars in model 1

local varsM1 "d_alter ant_eigReg ant_eigKonf ant_eigStand ant_eigBeruf 
	ant_eigVorp ant_eigParlAmt ant_eigPolAmt ant_eigVerein ant_eigStrafe" ;

#delimit cr

local varsM2 ""							// non-ideol vars in model 2: none

local clubs "Do De Wh Wü Au La Ca Mi" 	// base outcome ("none") not required


* Vars to store Coefs, SEs, and log-likekihoods from 100 runs of 
* models 1 and 2, respectively 

foreach name of local varsM1 {
	gen coef_`name'_M1 = .
	gen se_`name'_M1 = .			
}

forvalues i = 1/2 {						// one iteration per model

	foreach name of local ideol {
		gen coef_`name'_M`i' = .
		gen se_`name'_M`i' = .			
	}
	
	foreach name of local clubs {
		gen coef_`name'_M`i' = .
		gen se_`name'_M`i' = .			
	}	
	
	gen logL_M`i' = .
	gen logL0_M`i' = .
}


* Estimating models 1 and 2 and saving results
	
forvalues i = 1/2 {

	forvalues n = 1/100 {	

		display "Model `i', draw `n'"		// show progress

		quietly asclogit okt_best dist_med_part_draw`n' dist_med_kpart_draw`n' ///
		`varsM`i'' if auswahl==1, case(id) alt(alt) base(8)
		
		* Storing Coefs and SEs:
		
		foreach name of local ideol {		// ideology vars
			quietly replace coef_`name'_M`i' = _b[`name'_draw`n'] in `n'
			quietly replace se_`name'_M`i' = _se[`name'_draw`n'] in `n'
		}

		foreach name of local varsM`i' {	// non-ideology vars
			quietly replace coef_`name'_M`i' = _b[`name'] in `n'
			quietly replace se_`name'_M`i' = _se[`name'] in `n'
		}
		
		foreach name of local clubs {		// constants
			quietly replace coef_`name'_M`i' = _b[`name': _cons] in `n'
			quietly replace se_`name'_M`i' = _se[`name': _cons] in `n'
		}

		* Storing log-likelihoods:

		quietly replace logL_M`i' = e(ll) in `n'
		
		* restricted model: b_part = b_kpart
		quietly asclogit okt_best dist_med_all_draw`n' ///		
			`varsM`i'' if auswahl==1, case(id) alt(alt) base(8)

		quietly replace logL0_M`i' = e(ll) in `n'
	}
}

* Estimating model 3

asclogit okt_best d_alter ant_eigReg ant_eigKonf ant_eigStand ant_eigBeruf ///
	ant_eigVorp ant_eigParlAmt ant_eigPolAmt ant_eigVerein ant_eigStrafe ///
	if auswahl==1, case(id) alt(alt) base(8) 

est store mod3



* Combining Coefs and combining SEs and storing results in two matrices

matrix M1 = J(20, 4, .)
matrix M2 = J(10, 4, .)

forvalues i = 1/2 {

	matrix colnames M`i' = avg_coef avg_se lo95 up95

	local varnames "`ideol' `varsM`i'' `clubs'"
	local rnamesM`i' ""
	local n = 0

	foreach name of local varnames {
		
		local ++n
		local rnamesM`i' "`rnamesM`i'' `name'"

		* Rubin's Rule #1

		quietly sum coef_`name'_M`i'
		matrix M`i'[`n', 1] = r(mean)										
		
		* Rubin's Rule #2

		scalar Vcoef = r(Var)
		replace se_`name'_M`i' = (se_`name'_M`i')^2		// SEs into variances
		quietly sum se_`name'_M`i'
		matrix M`i'[`n', 2] = sqrt(r(mean) + Vcoef * (1 + 1 / 100))			
		
		* CIs

		matrix M`i'[`n', 3] = M`i'[`n', 1] + invnormal(0.025) * M`i'[`n', 2]
		matrix M`i'[`n', 4] = M`i'[`n', 1] + invnormal(0.975) * M`i'[`n', 2]		
	}

	matrix rownames M`i' = `rnamesM`i''
}

drop coef*_M? se_*_M?			// not needed anymore


* Results matrix for model 3 (this facilitates the creation of the
* coefficients plot below by ensuring consistent row names)

matrix M3 = J(18, 4, .)
matrix colnames M3 = coef se lo95 up95
matrix rownames M3 = `varsM1' `clubs'

est restore mod3

local n = 0
foreach name of local varsM1 {
	local ++n
	matrix M3[`n', 1] = _b[`name']
	matrix M3[`n', 2] = _se[`name']
	matrix M3[`n', 3] = M3[`n', 1] + invnormal(0.025) * M3[`n', 2]
	matrix M3[`n', 4] = M3[`n', 1] + invnormal(0.975) * M3[`n', 2]	
}
foreach name of local clubs {
	local ++n
	matrix M3[`n', 1] = _b[`name': _cons]
	matrix M3[`n', 2] = _se[`name': _cons]
	matrix M3[`n', 3] = M3[`n', 1] + invnormal(0.025) * M3[`n', 2]
	matrix M3[`n', 4] = M3[`n', 1] + invnormal(0.975) * M3[`n', 2]	
}



* Saving results
*******************

* Data 
save data_and_results.dta, replace

* Model 3 results
est restore mod3
est save model3, replace

* Analysis sample for further processing with R
export delim id alt idist_b_med_aug_part idist_b_med_aug_kpart d_alter ///
	ant_eigReg ant_eigKonf ant_eigStand ant_eigBeruf ant_eigVorp ///
	ant_eigParlAmt ant_eigPolAmt ant_eigVerein ant_eigStrafe ///
	Do De Wh Wü Au La Ca Mi using an_sample.csv if auswahl==1, replace

* Matrices with Coefs, SEs, and CIs from all models
forvalues i = 1/3 {
	clear
	svmat double M`i', names(col)
	save estimates_M`i'.dta, replace
}

* Coefficient vectors from all models for further processing with R
forvalues i = 1/3 {
	clear 
	matrix M`i' = (M`i')'
	svmat double M`i', names(col)
	keep if _n==1
	export delim using coefs_M`i'.csv, replace
}




